package monkey.myinterface;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchResult extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_result);
		int kindReceive;
		String SearchKey;
		Intent intent = getIntent();
		int index;
		kindReceive = intent.getIntExtra("kind", 0);

		ListView mList = (ListView) findViewById(R.id.search_result_list);
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		if (MessageManager.messages() != null) {
			for (Information info : MessageManager.messages()) {
				System.out.println(info.getOwnerName());
				HashMap<String, String> result = new HashMap<String, String>();
				switch (kindReceive) {
				case 0:
					SearchKey = intent.getStringExtra("name");
					index = info.getOwnerName().indexOf(SearchKey);
					if (index >= 0) {
						result.put("name", "Owner:"+info.getOwnerName()+"                                     "+"*MessageId*"+info.getId());
						result.put("tag", "Artist:"+info.getTag());
						list.add(result);

					}
					break;
				case 1:
					SearchKey = intent.getStringExtra("tag");
					System.out.println(SearchKey);
					index = info.getTag().indexOf(SearchKey);
					if (index >= 0) {
						result.put("name", "NickName:"+info.getOwnerName()+"                                 "+"*MessageId*"+info.getId());
						result.put("tag", "Tag"+info.getTag());
						list.add(result);
						break;
					}

				}
			}

		}

		SimpleAdapter listAdapter = new SimpleAdapter(this, list,
				R.layout.search_or_reply_result_list, new String[] { "name",
						"tag" }, new int[] { R.id.name, R.id.tag_or_message });
		if (list.isEmpty()) {
			HashMap<String, String> result = new HashMap<String, String>();
			result.put("name", "not match");
			list.add(result);
		}
		mList.setAdapter(listAdapter);
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				TextView mTextNameAndId = (TextView) arg1.findViewById(R.id.name);

				Intent mReplyIntent = new Intent();
				mReplyIntent.setClass(SearchResult.this, Reply.class);
				mReplyIntent.putExtra("Id", getId((String) mTextNameAndId.getText()));
				SearchResult.this.startActivity(mReplyIntent);

			}

		});

	}
	String getId(String str){
		System.out.println(str);
		int firstposition = str.indexOf("*MessageId*");
		System.out.println(str.substring(firstposition +11, str.length() )+"vvvvvvvvv");
		return str.substring(firstposition + 11, str.length());
	
	}

}
